import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class CollisionBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CollisionBar extends Actor
{
    private int posX;
    private int posY;
    private int type;
    
    private final int HORIZONTAL=0;
    private final int VERTICAL=1;
    
    public CollisionBar(int x,int y,int t)
    {
        GreenfootImage image=new GreenfootImage(10,10);
        /*image.setColor(Color.BLACK);
        image.fill();
        setImage(image);*/
        
        image.setColor(new Color(0,0,0,0));
        image.fill();
        setImage(image);
        
        posX=x;
        posY=y;
        type=t;
    }
        
        
    public void act() 
    {
        GameWorld world=(GameWorld)getWorld();
        Player player=world.getPlayer();
        
        if(type==HORIZONTAL)
        {
            setLocation(posX,player.getY()+posY);
        }else
            setLocation(player.getX()+posX,posY);          
    }    
    
    public boolean isTouchingGround()
    {
            if(isIntersecting(Ground.class))        
            return(true);
            else
            return(false);
    }
    
    private boolean isIntersecting(java.lang.Class cls)
    {
        java.util.List actors=getIntersectingObjects(Ground.class);
        
        if(actors.isEmpty())
        return(false);
        
        if(isVisuallyIntersecting((Actor)actors.get(0)))
        return(true);
        else
            if(actors.size()>1)
            return(isVisuallyIntersecting((Actor)actors.get(1)));
            
       return(false);
    }
    
    private boolean isVisuallyIntersecting(Actor actor)    
    {    
        // get width and height of images    
        int wA = getImage().getWidth();    
        int hA = getImage().getHeight();    
        int wB = actor.getImage().getWidth();    
        int hB = actor.getImage().getHeight();    
        // get world coordinates of origin point (0, 0) of images    
        int xAw = getX()-wA/2;    
        int yAw = getY()-hA/2;    
        int xBw = actor.getX()-wB/2;    
        int yBw = actor.getY()-hB/2;    
        // determine (x, y) on each image to begin checking from    
        // and dimensions of area to check    
        int xA = 0, yA = 0, xB = 0, yB = 0; // initialize start coord fields    
        int w = 0, h = 0; // initialize scan dimension fields    
        // determine values for fields just initialized    
        if (xAw > xBw)    
        {    
            xB = xAw-xBw;    
            if (xAw+wA > xBw+wB) w = wB-xB; else w = wA;    
        }    
        else    
        {    
            xA = xBw-xAw;    
            if (xBw+wB > xAw+wA) w = wA-xA; else w = wB;    
        }    
        if (yAw > yBw)    
        {    
            yB = yAw-yBw;    
            if (yAw+hA > yBw+hB) h = hB-yB; else h = hA;    
        }    
        else    
        {    
            yA = yBw-yAw;    
            if (yBw+hB > yAw+hA) h = hA-yA; else h = hB;    
        }    
        // perform checking    
        java.awt.Color trans = new java.awt.Color(0, 0, 0, 0);    
        for (int x=0; x<w; x++) for (int y=0; y<h; y++)    
            {    
                if (/*trans.equals(getImage().getColorAt(xA+x, yA+y)) || */trans.equals(actor.getImage().getColorAt(xB+x, yB+y)))   
                    return false;
        }    
        return true;    
    } 
}
